var form;
var ctxPath = '/'
$(function () {
    layui.config({
        base: ctxPath + 'lib/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index',//主入口模块
        treeTable: 'treetable-lay/2.x/treeTable'
    }).use(['index', 'form', 'treeTable'], function () {
        form = layui.form;
        var treeTable = layui.treeTable;
        // 渲染表格
        insTb = treeTable.render({
            elem: '#table',
            toolbar: '#table-title',
            reqData: function (data, callback) {
                $.ajax({
                    type: "GET",
                    url: "resourceList",
                    success: function (res) {
                        callback(res.obj);  // 参数是数组类型
                    }
                });
            },
            tree: {
                iconIndex: 0,  // 折叠图标显示在第几列
                idName: 'id', //设定id的字段名
                pidName: 'pid', //设定pid的字段名
                isPidData: true  // 是否是pid形式数据
            },
            cols: [
                {field: 'name', title: '资源名', width: 250, }, // class: 'break-all'
                {field: 'url', title: '资源地址', align: 'center', width: 200},
                {field: 'seq', title: '排序', width: 70, align: 'center'},
                {field: 'regroup', title: '功能分组', align: 'center', templet: '#isGroup'},
                {field: 'resourcetype', title: '资源类型', align: 'center', templet: '#isMenu'},
                {field: 'resourcetype', title: '警员资源', align: 'center', templet: '#isPolice'},
                {field: 'status', title: '是否启用', align: 'center', templet: '#islock'},
                {field: 'right', title: '操作', toolbar: '#table-toolbar', align: 'center'},
            ]
        });
        //监听行工具事件
        treeTable.on('tool(table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                openDel(obj)
            } else if (obj.event === 'edit') {
                openEdit(data)
            } else if (obj.event === 'add') {
                openAdd(data);
            }
        });
        form.on('submit(resourceSubmit)', function () {
            resourceSubmit();
        });
        //监听是否启用状态操作
        form.on('switch(islock)', function (obj) {
            setResourceStatus(obj, this.value, this.name, obj.elem.checked);
        });
    })
});

/**
 * 打开编辑页面
 * @param data layui中保存的table中的对应的此条信息
 */
function openEdit(data) {
    layer.open({
        type: 1,
        title: "资源管理",
        fixed: false,
        resize: false,
        shadeClose: true,
        area: ['35%', '80%'],
        content: $('#resourceEdit'),
        btn: ['确定', '取消'],
        success: function () {
            //填充表单
            form.val('resourceForm', data);
            renderForm()
        }, btn1: function (index) {
            resourceSubmit("1");
        }, end: function () {
            cleanForm("resourceForm");
        }
    });
}

function openAdd(data) {
    layer.open({
        type: 1,
        title: "新建资源",
        fixed: false,
        resize: false,
        shadeClose: true,
        area: ['35%', '80%'],
        content: $('#resourceEdit'),
        btn: ['确定', '取消'],
        btn1: function (index) {
            //写入父资源ID
            $("input[name='pid']").val(data.id);
            resourceSubmit("0");
        }, end: function () {
            cleanForm("resourceForm");
        }
    })
}

/**
 * 提交表单
 * @param oper 0：新增  1：更新
 */
function resourceSubmit(oper) {
    var type = oper == "0" ? "POST" : "PUT";
    $.ajax({
        type: type,
        data: serializeObject($("#resourceForm")),
        url: ctxPath + "config/resource/resourceInfo",
        complete: function () {
            insTb.refresh();
        }
    });
}

/**
 * 打开删除框
 * @param obj
 */
function openDel(obj) {
    var data = obj.data
    layer.confirm('此操作将会删除' + data.name + '以及其下所有资源，请慎重操作', function () {
        $.ajax({
            type: "DELETE",
            data: {"id": data.id},
            url: ctxPath + "config/resource/resourceInfo",
            complete: function () {
                insTb.refresh();
            }
        });
    });
}

function setResourceStatus(obj, id, name, checked) {
    var status = checked ? 0 : 1;
    var ParaIsLock = checked ? "启用" : "停用";
    //用户状态
    layer.confirm('此操作将会' + ParaIsLock + name + '及其所有子资源', {
        btn: ['确认', '返回'] //按钮
        //点击关闭按钮事件
        , cancel: function () {
            insTb.refresh();
        }
    }, function () {
        $.ajax({
            type: "PUT",
            data: {"id": id, "status": status},
            url: ctxPath + "config/resource/status",
            complete: function () {
                insTb.refresh();
            }
        });
    }, function () {
        insTb.refresh();
    });
}
